﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FooFun.DataElements.Dtos
{
    public class ArrayDataElement : DataElement
    {
        Array array;

        public ArrayDataElement(Array array)
            : base(array)
        {

        }

        public override object Value
        {
            get
            {
                return array;
            }
            set
            {
                array = (Array)value;
            }
        }

        public override IEnumerator GetEnumerator()
        {
            foreach (object obj in array)
            {
                if (obj is IDataElement)
                {
                    yield return (IDataElement)obj;
                }
                else
                {
                    yield return new ObjectDataElement(obj);
                }
            }
        }

        public override IDataElement this[int index]
        {
            get
            {
                return new ObjectDataElement
                    (
                    array.GetValue(index)
                    );
            }
            set
            {
                array.SetValue(value.Value, index);
            }
        }

        public override bool IsEmpty
        {
            get
            {
                if (array != null)
                {
                    return array.Length == 0;
                }
                return true;
            }
        }
    }
}
